package day02;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.JdbcUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/list")
public class EmpListServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("EmpListServlet.service");

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        PrintWriter out=response.getWriter();

        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con= JdbcUtil.getConnection();
            con.setAutoCommit(false);


            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_emp ")
                    .toString();

            ps=con.prepareStatement(sql);


            rs=ps.executeQuery();

            out.println("<!DOCTYPE html>");
            out.println("<html lang=\"en\">");
            out.println("<head>");
            out.println("    <meta charset=\"UTF-8\">");
            out.println("    <title>Title</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("    <table border=\"1\" width=\"50%\">");
            out.println("        <tr>");
            out.println("            <th>序号</th>");
            out.println("            <th>姓名</th>");
            out.println("            <th>工资</th>");
            out.println("            <th>操作</th>");
            out.println("        </tr>");

            int i=1;
            while(rs.next()){
                Integer id=rs.getInt("id");
                String name=rs.getString("name");
                Double salary=rs.getDouble("salary");

                System.out.println(id+"\t"+name+"\t"+salary);


                out.println("        <tr>");
                out.println("            <td>"+(i++)+"</td>");
                out.println("            <td>");
                out.println("                <a href=\""+request.getContextPath()+"/showModify?id="+id+"\">"+name+"</a>");
                out.println("            </td>");
                out.println("            <td>"+salary+"</td>");
                out.println("            <td>");
                out.println("                <a href=\""+request.getContextPath()+"/remove?id="+id+"\" onclick=\"return confirm('确认是否删除？');\">删除</a>");
                out.println("            </td>");
                out.println("        </tr>");



            }

            out.println("    </table>");
            out.println("</body>");
            out.println("</html>");




            con.commit();
        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally{
            JdbcUtil.close(rs,ps,con);
        }




    }
}
